import { computed, ref } from 'vue'

// 折线图数据接口
interface LineChartData {
  categories: string[]
  series: Array<{
    name: string
    data: number[]
    color?: string
  }>
}

// 饼图数据接口
interface PieChartData {
  name: string
  value: number
  color?: string
}

// 柱状图数据接口
interface BarChartData {
  categories: string[]
  series: Array<{
    name: string
    data: number[]
    color?: string
  }>
}

// 甘特图数据接口
interface GanttChartData {
  categories: string[]
  series: Array<{
    name: string
    data: number[]
    color?: string
  }>
}

export function useCharts() {
  // 折线图数据 - 年度销售趋势
  const lineChartData = ref<LineChartData>({
    categories: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
    series: [
      {
        name: '2024年销售额',
        data: [820, 932, 901, 934, 1290, 1330, 1320, 1450, 1380, 1520, 1680, 1750],
        color: '#1890ff',
      },
      {
        name: '2023年销售额',
        data: [680, 792, 750, 834, 1090, 1130, 1120, 1250, 1180, 1320, 1480, 1550],
        color: '#52c41a',
      },
      {
        name: '目标销售额',
        data: [900, 1000, 950, 1000, 1300, 1400, 1350, 1500, 1400, 1600, 1700, 1800],
        color: '#faad14',
      },
    ],
  })

  // 饼图数据 - 用户来源分析
  const pieChartData = ref<PieChartData[]>([
    { name: '移动端用户', value: 45.2, color: '#1890ff' },
    { name: '桌面端用户', value: 28.6, color: '#52c41a' },
    { name: '平板端用户', value: 15.8, color: '#faad14' },
    { name: '微信小程序', value: 7.3, color: '#f5222d' },
    { name: '其他渠道', value: 3.1, color: '#722ed1' },
  ])

  // 柱状图数据 - 周销售对比
  const barChartData = ref<BarChartData>({
    categories: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
    series: [
      {
        name: '本周销量',
        data: [320, 302, 341, 374, 390, 450, 420],
        color: '#1890ff',
      },
      {
        name: '上周销量',
        data: [280, 252, 301, 334, 350, 380, 360],
        color: '#52c41a',
      },
      {
        name: '目标销量',
        data: [350, 350, 350, 350, 350, 350, 350],
        color: '#faad14',
      },
    ],
  })

  // 甘特图数据 - 项目进度管理
  const ganttChartData = ref<GanttChartData>({
    categories: ['需求分析', '系统设计', '前端开发', '后端开发', '测试验收', '部署上线'],
    series: [
      {
        name: '计划进度',
        data: [100, 95, 75, 80, 45, 20],
        color: '#1890ff',
      },
      {
        name: '实际进度',
        data: [100, 90, 68, 72, 35, 10],
        color: '#52c41a',
      },
      {
        name: '延期风险',
        data: [0, 5, 7, 8, 10, 10],
        color: '#f5222d',
      },
    ],
  })

  // 加载图表数据
  function loadChartsData() {
    // 模拟异步加载数据
    setTimeout(() => {
      console.log('图表数据加载完成')
    }, 500)
  }

  // 刷新图表数据
  function refreshChartsData() {
    // 模拟刷新折线图数据 - 添加随机波动
    const baseData2024 = [820, 932, 901, 934, 1290, 1330, 1320, 1450, 1380, 1520, 1680, 1750]
    const baseData2023 = [680, 792, 750, 834, 1090, 1130, 1120, 1250, 1180, 1320, 1480, 1550]
    const baseTarget = [900, 1000, 950, 1000, 1300, 1400, 1350, 1500, 1400, 1600, 1700, 1800]

    lineChartData.value = {
      categories: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
      series: [
        {
          name: '2024年销售额',
          data: baseData2024.map(val => Math.floor(val + (Math.random() - 0.5) * 200)),
          color: '#1890ff',
        },
        {
          name: '2023年销售额',
          data: baseData2023.map(val => Math.floor(val + (Math.random() - 0.5) * 150)),
          color: '#52c41a',
        },
        {
          name: '目标销售额',
          data: baseTarget,
          color: '#faad14',
        },
      ],
    }

    // 刷新饼图数据 - 保持总和为100%
    const total = 100
    const values = [
      Math.floor(Math.random() * 20) + 35, // 35-55
      Math.floor(Math.random() * 15) + 20, // 20-35
      Math.floor(Math.random() * 10) + 10, // 10-20
      Math.floor(Math.random() * 8) + 5, // 5-13
    ]
    const remaining = total - values.reduce((sum, val) => sum + val, 0)
    values.push(Math.max(remaining, 1))

    pieChartData.value = [
      { name: '移动端用户', value: values[0], color: '#1890ff' },
      { name: '桌面端用户', value: values[1], color: '#52c41a' },
      { name: '平板端用户', value: values[2], color: '#faad14' },
      { name: '微信小程序', value: values[3], color: '#f5222d' },
      { name: '其他渠道', value: values[4], color: '#722ed1' },
    ]

    // 刷新柱状图数据
    const baseSales = [320, 302, 341, 374, 390, 450, 420]
    const lastWeekSales = [280, 252, 301, 334, 350, 380, 360]

    barChartData.value = {
      categories: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
      series: [
        {
          name: '本周销量',
          data: baseSales.map(val => Math.floor(val + (Math.random() - 0.5) * 100)),
          color: '#1890ff',
        },
        {
          name: '上周销量',
          data: lastWeekSales.map(val => Math.floor(val + (Math.random() - 0.5) * 80)),
          color: '#52c41a',
        },
        {
          name: '目标销量',
          data: [350, 350, 350, 350, 350, 350, 350],
          color: '#faad14',
        },
      ],
    }

    // 刷新甘特图数据 - 模拟项目进度变化
    const planProgress = [100, 95, 75, 80, 45, 20]
    const actualProgress = [100, 90, 68, 72, 35, 10]

    ganttChartData.value = {
      categories: ['需求分析', '系统设计', '前端开发', '后端开发', '测试验收', '部署上线'],
      series: [
        {
          name: '计划进度',
          data: planProgress,
          color: '#1890ff',
        },
        {
          name: '实际进度',
          data: actualProgress.map((val, index) => {
            if (index < 2)
              return val // 已完成的不变
            return Math.max(0, Math.min(100, val + Math.floor((Math.random() - 0.5) * 20)))
          }),
          color: '#52c41a',
        },
        {
          name: '延期风险',
          data: planProgress.map((plan, index) => {
            const actual = actualProgress[index]
            return Math.max(0, plan - actual + Math.floor(Math.random() * 5))
          }),
          color: '#f5222d',
        },
      ],
    }
  }

  // 更新折线图数据
  function updateLineChartData(newData: LineChartData) {
    lineChartData.value = newData
  }

  // 更新饼图数据
  function updatePieChartData(newData: PieChartData[]) {
    pieChartData.value = newData
  }

  // 更新柱状图数据
  function updateBarChartData(newData: BarChartData) {
    barChartData.value = newData
  }

  // 更新甘特图数据
  function updateGanttChartData(newData: GanttChartData) {
    ganttChartData.value = newData
  }

  // 检查是否有数据
  const hasData = computed(() => {
    return (
      lineChartData.value.categories.length > 0
      || pieChartData.value.length > 0
      || barChartData.value.categories.length > 0
      || ganttChartData.value.categories.length > 0
    )
  })

  return {
    // 数据
    lineChartData,
    pieChartData,
    barChartData,
    ganttChartData,

    // 方法
    loadChartsData,
    refreshChartsData,
    updateLineChartData,
    updatePieChartData,
    updateBarChartData,
    updateGanttChartData,

    // 计算属性
    hasData,
  }
}
